﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Anay.TaskScheduling.JobConfig;

namespace Anay.TaskScheduling.Common
{
    public class ConfigJsonHelper : IConfigHelper
    {

        public static List<Ep_task_config> ConfigList = null;
        /// <summary>
        /// 配置文件名
        /// </summary>
        static string ConfigFileName = "TaskConfig.json";

        static string ConfigFilePath = System.AppDomain.CurrentDomain.BaseDirectory;
        static string ConfigLasrRunTimePath = ConfigFilePath + "log/LastRunTime/{0}/";

        public ConfigJsonHelper()
        {
            //没有配置文件 或者配置文件个数为0 重新获取下
            GetAllList();
        }

        public List<Ep_task_config> GetAllList()
        {
            if (ConfigList == null || ConfigList.Count == 0)
            {
                ConfigList = Newtonsoft.Json.JsonConvert.DeserializeObject<List<Ep_task_config>>(System.IO.File.ReadAllText(ConfigFilePath + ConfigFileName));
            }
            return ConfigList;
        }

        public List<Ep_task_config> GetTaskConfigList(bool IsEnabled = true)
        {
            if (!IsEnabled)
                return ConfigList.Where(c => c.IsEnabled == "1").ToList();

            return ConfigList.Where(c => c.IsEnabled == "0").ToList();
        }

        public Ep_task_config GetTask_config(string id)
        {
            return ConfigList.Where(c => c.Id == id).ToList().FirstOrDefault();
        }

        public Ep_task_config GetTask_ConfigByJobName(string name)
        {
            return ConfigList.Where(c => c.JobName == name).ToList().FirstOrDefault();
        }

        public bool UpdateLastRunTime(Ep_task_config task_Config)
        {
            bool result;
            try
            {
                var lastRunTimePath = string.Format(ConfigLasrRunTimePath, task_Config.JobName);
                if (!Directory.Exists(lastRunTimePath))
                {
                    Directory.CreateDirectory(lastRunTimePath);
                }
                var FilePath = lastRunTimePath + "lastRuntime.txt";
                if (File.Exists(FilePath))
                {
                    File.WriteAllText(FilePath, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                }
                else
                {
                    File.AppendAllText(FilePath, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                }
                result = true;
            }
            catch (Exception ex)
            {
                result = false;
            }

            return result;
        }

        public bool UpdateTime(Ep_task_config task_Config)
        {
            throw new NotImplementedException();
        }

        public DateTime? GetLastRunTime(string id)
        {
            var task_Config = GetTask_config(id);
            var lastRunTimePath = string.Format(ConfigLasrRunTimePath, task_Config.JobName);
            var dateString="";
            try
            {
                 dateString= File.ReadAllText(lastRunTimePath+ "\\lastRuntime.txt");
                if (string.IsNullOrWhiteSpace(dateString))
                {
                    dateString = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                }
               
            }
            catch (Exception ex)
            {
                dateString =null;
            }
            if (string.IsNullOrEmpty(dateString)) return null;
            return Convert.ToDateTime(dateString);
        }

        public void RefreshConfigs()
        {
            ConfigList = null;
            GetAllList();
        }

        public void AddConfig(Ep_task_config task_Config)
        {
            ConfigList.Add(task_Config);
            System.IO.File.WriteAllText(ConfigFilePath + ConfigFileName, Newtonsoft.Json.JsonConvert.SerializeObject(ConfigList));
        }

        public bool DeleteConfig(string id)
        {
            var taskConfig= GetTask_config(id);
            if (taskConfig==null)
            {
                return false;
            }
            ConfigList.Remove(taskConfig);
            System.IO.File.WriteAllText(ConfigFilePath + ConfigFileName, Newtonsoft.Json.JsonConvert.SerializeObject(ConfigList));
            return true;
        }

        public void UpdateConfig()
        {
            if (ConfigList.Count>0)
            {
                System.IO.File.WriteAllText(ConfigFilePath + ConfigFileName, Newtonsoft.Json.JsonConvert.SerializeObject(ConfigList));
            }
           
        }
    }
}
